/**
 * 
 */
package net.howson.phil.datahiding.stream;

import java.io.IOException;
import java.io.InputStream;
import java.nio.ByteBuffer;

public class HeadedInputStream extends InputStream {
	private final ByteBuffer byteBuffer = ByteBuffer.allocate(8);
	private final InputStream delegate;

	public HeadedInputStream(long inputSize, int repititions,
			InputStream delegate) {
		byteBuffer.putInt((int) inputSize);
		byteBuffer.putInt(repititions);
		byteBuffer.position(0);
		this.delegate = delegate;
	}

	@Override
	public int read() throws IOException {
		if (byteBuffer.hasRemaining()) {
			return byteBuffer.get();
		}
		return delegate.read();
	}

	@Override
	public void close() throws IOException {
		delegate.close();
		super.close();
	}

}